# -*- coding: utf-8 -*-
# @Time : 2022/5/14 00:45
# @Author : cheney
# @File : socket_test_a.py
# @Software: PyCharm
# @Site: www.cheney.cc

import mysocket
import socket
import mysocket
import socket
import packet
import des2
import rsa2

publicKey, privateKey = rsa2.newkeys(512)
sessionKey = des2.newkey()
print(sessionKey)

a_port = 11124

if __name__ == '__main__':
    as_Socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)  # 创建套接字
    as_Socket.bind(("127.0.0.1", a_port))  # 将套接字与端口号绑定起来
    as_Socket.listen(5)  # socket的”排队个数“为5 ！！！不是最大连接数

    while True:
        print("正在listen：", as_Socket)
        new_sock, adress = as_Socket.accept()  # 这条命令是阻塞的
        print("收到连接", new_sock)

        a = packet.get_pack("c_a", content="0", sessionkey=11651596744076885038, privatekey=privateKey)
        print("待发送的报文长度为：len(a)", len(a))
        print(">>>正常的des加密有：", len(des2.encrypt("0", sessionKey)))
        # ret = new_sock.send(a)
        # print("ret", ret)
        mysocket.send(new_sock, a, ack_flag=True)

        # while True:
        #     recvv = new_sock.recv(10)
        #     if recvv == b"":
        #         print("断开\n")
        #         break
        #     else:
        #         print("收到:", len(recvv), recvv.decode())
        #     new_sock.send(("0"*100).encode())
